package com.takeoutOrder.server.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.takeoutOrder.common.utils.DateUtils;
import com.takeoutOrder.server.domain.Dish;
import com.takeoutOrder.server.domain.SetmealDish;
import com.takeoutOrder.server.domain.dto.SetmealDishDto;
import com.takeoutOrder.server.mapper.DishMapper;
import com.takeoutOrder.server.mapper.SetmealDishMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.takeoutOrder.server.mapper.SetmealMapper;
import com.takeoutOrder.server.domain.Setmeal;
import com.takeoutOrder.server.service.ISetmealService;
import org.springframework.transaction.annotation.Transactional;

import static com.takeoutOrder.common.utils.DateUtils.parseDate;

/**
 * 套餐管理Service业务层处理
 * 
 * @author linzechen
 * @date 2024-11-25
 */
@Service
public class SetmealServiceImpl implements ISetmealService 
{
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 查询套餐管理
     * 
     * @param id 套餐管理主键
     * @return 套餐管理
     */
    @Override
    public SetmealDishDto selectSetmealById(Long id)
    {
        //在此处查询套餐信息,以及通过套餐菜品表查询菜品信息,然后添加到SetmealDishDto中
        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        SetmealDishDto setmealDishDto = BeanUtil.copyProperties(setmeal, SetmealDishDto.class);
       List<SetmealDish> setmealDishes=setmealDishMapper.selectBySetmealId(setmealDishDto.getId());
        setmealDishDto.setDishList(setmealDishes);
        return setmealDishDto;
    }

    /**
     * 查询套餐管理列表
     * 
     * @param setmeal 套餐管理
     * @return 套餐管理
     */
    @Override
    public List<Setmeal> selectSetmealList(Setmeal setmeal)
    {
        return setmealMapper.selectSetmealList(setmeal);
    }

    /**
     * 新增套餐管理
     * 
     * @param setmeal 套餐管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSetmeal(SetmealDishDto setmealDishDto)
    {
        Setmeal setmeal = BeanUtil.copyProperties(setmealDishDto, Setmeal.class);
        //先插入套餐信息,然后才能得到回显的id
        int result = setmealMapper.insertSetmeal(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishList = setmealDishDto.getDishList().stream().map(item->{
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        setmealDishMapper.insertAll(setmealDishList);
        setmeal.setCreateTime(DateUtils.getNowDate());
        return result;
    }

    /**
     * 修改套餐管理
     * 
     * @param setmeal 套餐管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSetmeal(SetmealDishDto setmealDishDto)
    {
        //获取套餐对象和套餐菜品关联集合对象
        Setmeal setmeal = BeanUtil.copyProperties(setmealDishDto, Setmeal.class);
        Long setmealId = setmeal.getId();
            List<SetmealDish> setmealDishList = setmealDishDto.getDishList().stream().map(item->{
                item.setSetmealId(setmealId);
                return item;
            }).collect(Collectors.toList());
            setmealDishMapper.deleteBySetmealId(setmealId);
            setmealDishMapper.insertAll(setmealDishList);
        setmeal.setUpdateTime(parseDate(DateUtils.dateTimeNow()));
        return setmealMapper.updateSetmeal(setmeal);
    }

    /**
     * 批量删除套餐管理
     * 
     * @param ids 需要删除的套餐管理主键
     * @return 结果
     */
    @Override
    public int deleteSetmealByIds(Long[] ids)
    {
        return setmealMapper.deleteSetmealByIds(ids);
    }

    /**
     * 删除套餐管理信息
     * 
     * @param id 套餐管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSetmealById(Long id)
    {
        setmealDishMapper.deleteBySetmealId(id);
        return setmealMapper.deleteSetmealById(id);
    }

    /**
     * 修改套餐状态
     * @param setmeal
     * @return 影响行数
     */
    @Override
    public int updateSetmealStatus(Setmeal setmeal) {
        return setmealMapper.updateSetmeal(setmeal);
    }
}
